import router from '@/router'
import store from '@/store'
// 导入进度条
import NProgress from 'nprogress'
// 导入进度条的样式
import 'nprogress/nprogress.css'
// 动态设置标题
import getPageTitle from '@/utils/get-page-title'
// 设置路由白名单
const whiteList = ['/login', '/404']
router.beforeEach((to, from, next) => {
  console.log(to.path, '去哪里')
  console.log(from.path, '从哪里来')
  console.log(to, '123123123')
  document.title = getPageTitle(to.meta.title)
  NProgress.start()
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      console.log('您已经登录了不能跳转到登录页')
      next('/')
      NProgress.done()
    } else {
      console.log('您已经登录了放行')
      if (!store.state.user.userInfo.userId) {
        store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      console.log('您是路由白名单')
      next()
    } else {
      console.log('您不是白名单')
      next('/login')
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
